package com.sita.tboard.ui.fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sita.bike.R;
import com.sita.bike.ui.activity.roadtrust.RoadTrustShareActivity;
import com.sita.bike.utils.LogUtils;
import com.sita.tboard.roadtrust.discovery.RtResourceListFragment;

public class DiscoveryFragment extends Fragment {
    private final String TAG = DiscoveryFragment.class.getSimpleName();
    Resources resources;
    private TabLayout mTabLayout;

    protected static final int TAB_STATE = 0;
    protected static final int TAB_PEOPLE = 1;
    protected static final int TAB_COUNT = 1;
    protected static final int TAB_START = 0;
    private int currIndex = -1;
    private ImageView shareRoadTrust;
    private DiscoveryPeopleFragment mPeopleFragment = null;
    private RtResourceListFragment mTrendListFragment = null;
    private Fragment mCurrentFragment = null;
    private boolean mViewCreated = false;
    private int mInitTab = TAB_START;
    private boolean mIsCalled = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discovery_new, null);
        shareRoadTrust = (ImageView) view.findViewById(R.id.btn_road_trust_share);
        shareRoadTrust.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), RoadTrustShareActivity.class);
                startActivity(intent);
            }
        });
        resources = getResources();
        InitViewPager(view);
        mViewCreated = true;
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabLayout.getTabAt(mInitTab).select();
        if (mIsCalled && mCurrentFragment instanceof RtResourceListFragment) {
            mTrendListFragment.onRefresh();
            mIsCalled = false;
        }
        
    }

    public void showTrendTab(String resourceId) {
        if (mViewCreated) {
            mTabLayout.getTabAt(TAB_STATE).select();
            mTrendListFragment.onRefresh();
        } else {
            mIsCalled = true;
            mInitTab = TAB_STATE;
        }
    }

    private void InitViewPager(View parentView) {
        mTabLayout = (TabLayout) parentView.findViewById(R.id.tab_layout);
        mTabLayout.addTab(mTabLayout.newTab().setText(R.string.discovery_state));
//        mTabLayout.addTab(mTabLayout.newTab().setText(R.string.discovery_people));
        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        mTabLayout.setOnTabSelectedListener(new MyTabOnClickListener());

    }

    private class MyTabOnClickListener implements TabLayout.OnTabSelectedListener {
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            LogUtils.d(TAG, "tab reselected @" + tab.getPosition());
            selectFragment(tab.getPosition());
        }

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            LogUtils.d(TAG, "tab selected @" + tab.getPosition());
            selectFragment(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            LogUtils.d(TAG, "tab unselected @" + tab.getPosition());
        }
    }

    private Fragment selectFragment(int index) {
        if (currIndex == index || index < TAB_START || index >= TAB_COUNT) {
            return mCurrentFragment;
        }
        shareRoadTrust.setVisibility((index == TAB_STATE) ? View.VISIBLE : View.GONE);
        // change the fragment
        FragmentTransaction ftx = getChildFragmentManager().beginTransaction();
        Fragment lastFragment = getFragment(currIndex);
        if (lastFragment != null) {
            ftx = ftx.hide(lastFragment);
        }
        Fragment currentFragment = getFragment(currIndex = index);
        if (!currentFragment.isAdded()) {
            ftx = ftx.add(R.id.frame_container_discovery, currentFragment, String.valueOf(index));
        }
        ftx.show(currentFragment).commit();
        return currentFragment;

//        // change the fragment
//        FragmentTransaction ftx = getChildFragmentManager().beginTransaction();
//        ftx.replace(R.id.frame_container_discovery, fragment).commit();

    }

    private Fragment getFragment(int index) {
        Fragment fragment = null;
        switch (index) {
            case TAB_PEOPLE:
                if (mPeopleFragment == null) {
                    mPeopleFragment = new DiscoveryPeopleFragment();
                }
                fragment = mPeopleFragment;
                break;
            case TAB_STATE:
                if (mTrendListFragment == null) {
                    mTrendListFragment = new RtResourceListFragment();
                }
                fragment = mTrendListFragment;
                break;
        }
        mCurrentFragment = fragment;
        return fragment;

    }

}
